//
//  ChinaVC.m
//  K-Music
//
//  Created by CHUN on 16/1/1.
//  Copyright © 2016年 CHUN. All rights reserved.
//

#import "ChinaVC.h"

#import "SameWay.h"

#import "UIImageView+WebCache.h"

#import "WHScrollAndPageView.h"

static NSString * const CellIdentifier = @"RushCvCell";
static NSString * const HeaderIdentifier = @"HeaderIdentifier";

@interface ChinaVC ()<UIScrollViewDelegate,WHcrollViewViewDelegate>{
    
    UICollectionView*RustVC;
    
//    NSMutableArray * all_Data;
    
    WHScrollAndPageView *_whView;
    
}

@end

@implementation ChinaVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.navigationItem.title = @"全国榜";
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithIcon:@"play" highIcon:@"play" target:self action:@selector(play_play)];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(back_back)];
    
    [self creatVC];
    

}

- (void)play_play
{
    NSLog(@"play");
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"music_play" object:self];
}

- (void)back_back
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void )creatVC{
    
    
    self.collectionViewLayout = [[RAMCollectionViewFlemishBondLayout alloc] init];
    self.collectionViewLayout.delegate = self;
    self.collectionViewLayout.numberOfElements = 2;
    self.collectionViewLayout.highlightedCellHeight = HEIGHT/4;
    self.collectionViewLayout.highlightedCellWidth = WIDTH/2;
    
    RustVC = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0, WIDTH, HEIGHT-64) collectionViewLayout:self.collectionViewLayout];
    
    RustVC.delegate = self;
    RustVC.dataSource = self;
    RustVC.backgroundColor = [UIColor whiteColor];
    
    [RustVC registerClass:[RushCvCell class] forCellWithReuseIdentifier:CellIdentifier];
    [RustVC registerClass:[RAMCollectionAuxView class] forSupplementaryViewOfKind:RAMCollectionViewFlemishBondHeaderKind withReuseIdentifier:HeaderIdentifier];
    
    [self.view addSubview:RustVC];
    
}



#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [User_information conversion].QGB_Data.count - 1;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    [RustVC registerClass:[RushCvCell class] forCellWithReuseIdentifier:@"RushCvCell"];
    
    RushCvCell*cell=(RushCvCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"RushCvCell" forIndexPath:indexPath];
    
    A_Music * data =[[A_Music alloc]init];
    
    data  = [User_information conversion].QGB_Data[indexPath.row+1];
    
    //1头像  底图片
    [cell.icon_image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",data.avatar]] placeholderImage:
     nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
         
         cell.icon_image.image = [cell.icon_image.image imageByCroppingWithStyle:10];
         
     }];
    
    //2序号
    cell.list_num_label.text = [NSString stringWithFormat:@"%ld.",(long)indexPath.row+2
                                ];
    
    //3 歌曲名
    cell.song_name.text = [NSString stringWithFormat:@"%@",data.name];
    
    //4 用户名 + 3 图标
    //    cell.my_name.text=[NSString stringWithFormat:@"%@",data.userId];
    
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",data.userId]];
    
    //(1)VIP icon
    // 添加表情
    NSTextAttachment *vip = [[NSTextAttachment alloc] init];
    // 设置图片大小
    vip.bounds = CGRectMake(2, 0, 25, 12.5);
    // 表情图片
    vip.image = [UIImage imageNamed:@"vip_one"];
    
    // 创建带有图片的富文本
    NSAttributedString * vip_Icon = [NSAttributedString attributedStringWithAttachment:vip];
    
    [attri appendAttributedString:vip_Icon];
    
    //(2)fortune
    NSTextAttachment * fortune = [[NSTextAttachment alloc]init];
    
    fortune.bounds = CGRectMake(2, -5, 25, 25);
    
    [cell.star_one sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",data.fortuneLevelIcon]]];
    
    fortune.image = cell.star_one.image ;
    
    NSAttributedString * fortuneLevel_Icon = [NSAttributedString attributedStringWithAttachment:fortune];
    
    [attri appendAttributedString:fortuneLevel_Icon];
    
    //(3)singer
    NSTextAttachment * singer = [[NSTextAttachment alloc]init];
    
    singer.bounds = CGRectMake(2, -5, 25, 25);
    
    [cell.star_two sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",data.singerLevelIcon]]];
    
    singer.image = cell.star_two.image ;
    
    NSAttributedString * singerLevel_Icon = [NSAttributedString attributedStringWithAttachment:singer];
    
    [attri appendAttributedString:singerLevel_Icon];
    
    // 用label的attributedText属性来使用富文本
    cell.user_name.attributedText = attri;
    
    return cell;

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"%ld",indexPath.row);
    YYplayerVC*yy=[[YYplayerVC alloc]init];
    
    yy.hidesBottomBarWhenPushed = YES;
    
    yy.musicData= [User_information conversion].musicData[indexPath.row];
    
    [yy setTracks:[Track remoteTracks]];
    [self.navigationController pushViewController:yy animated:NO];
    
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
{
    UICollectionReusableView * Header_View;
    
    if (kind == RAMCollectionViewFlemishBondHeaderKind) {
        
        Header_View = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:HeaderIdentifier forIndexPath:indexPath];
        
        A_Music * data =[[A_Music alloc]init];
        
        data  = [User_information conversion].Area_Data[0];
        
        //1头像  底图片
        [((RAMCollectionAuxView *)Header_View).icon_image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",data.avatar]] placeholderImage:
         nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
             
             ((RAMCollectionAuxView *)Header_View).icon_image.image = [((RAMCollectionAuxView *)Header_View).icon_image.image imageByCroppingWithStyle:10];
             
         }];
        
        //2序号
        ((RAMCollectionAuxView *)Header_View).list_num_label.text = [NSString stringWithFormat:@"%ld.",(long)indexPath.row+1
                                                                     ];
        //3 歌曲名
        ((RAMCollectionAuxView *)Header_View).song_name.text = [NSString stringWithFormat:@"%@",data.name];
        
        //4 用户名 + 3 图标
        //    cell.my_name.text=[NSString stringWithFormat:@"%@",data.userId];
        
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",data.userId]];
        
        //(1)VIP icon
        // 添加表情
        NSTextAttachment *vip = [[NSTextAttachment alloc] init];
        // 设置图片大小
        vip.bounds = CGRectMake(2, 0, 25, 12.5);
        // 表情图片
        vip.image = [UIImage imageNamed:@"vip_one"];
        
        // 创建带有图片的富文本
        NSAttributedString * vip_Icon = [NSAttributedString attributedStringWithAttachment:vip];
        
        [attri appendAttributedString:vip_Icon];
        
        //(2)fortune
        NSTextAttachment * fortune = [[NSTextAttachment alloc]init];
        
        fortune.bounds = CGRectMake(2, -5, 25, 25);
        
        [((RAMCollectionAuxView *)Header_View).star_one sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",data.fortuneLevelIcon]]];
        
        fortune.image = ((RAMCollectionAuxView *)Header_View).star_one.image ;
        
        NSAttributedString * fortuneLevel_Icon = [NSAttributedString attributedStringWithAttachment:fortune];
        
        [attri appendAttributedString:fortuneLevel_Icon];
        
        //(3)singer
        NSTextAttachment * singer = [[NSTextAttachment alloc]init];
        
        singer.bounds = CGRectMake(2, -5, 25, 25);
        
        [ ((RAMCollectionAuxView *)Header_View).star_two sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",data.singerLevelIcon]]];
        
        singer.image = ((RAMCollectionAuxView *)Header_View).star_two.image ;
        
        NSAttributedString * singerLevel_Icon = [NSAttributedString attributedStringWithAttachment:singer];
        
        [attri appendAttributedString:singerLevel_Icon];
        
        // 用label的attributedText属性来使用富文本
        ((RAMCollectionAuxView *)Header_View).user_name.attributedText = attri;
        
    }
    
    return Header_View;
    
}
#pragma mark - RAMCollectionViewVunityLayoutDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(RAMCollectionViewFlemishBondLayout *)collectionViewLayout estimatedSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(CGRectGetWidth(RustVC.frame), HEIGHT/4+20);
}

- (RAMCollectionViewFlemishBondLayoutGroupDirection)collectionView:(UICollectionView *)collectionView layout:(RAMCollectionViewFlemishBondLayout *)collectionViewLayout highlightedCellDirectionForGroup:(NSInteger)group atIndexPath:(NSIndexPath *)indexPath
{
    RAMCollectionViewFlemishBondLayoutGroupDirection direction;
    
    if (indexPath.row % 2) {
        direction = RAMCollectionViewFlemishBondLayoutGroupDirectionRight;
    } else {
        direction = RAMCollectionViewFlemishBondLayoutGroupDirectionLeft;
    }
    
    return direction;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
